﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Kiosk.Library;
using System.Windows.Media.Animation;
using Kiosk.Animation;

namespace Kiosk.UserControls
{
    /// <summary>
    /// Interaction logic for HomeScreen.xaml
    /// </summary>
    public partial class HomeScreen : BaseKioskControl
    {
        private bool _showingPresentation = true;

        public HomeScreen()
        {
            
            InitializeComponent();
        }

        private void notification_Selected(object sender, NotificationSelectEventArgs e)
        {
            NotificationListPresenter uc = (NotificationListPresenter)sender;
            uc.ClearSelection();
            NotificationPresenter np = new NotificationPresenter(e.Notification) { Width = double.NaN, Height = double.NaN };
            np.Closed += delegate(object s, EventArgs ev )
            {
                ShowPresentation();
                EnableNotifications();
            };

            DisableNotifications();
            PausePresentation();
            brdContainer.Child = np;
        }

        #region Notification Methods
		private void AnimateNotifications(DependencyProperty targetProp, AnimationTimeline animation )
        {
            ucEvents.BeginAnimation(targetProp, animation);
            ucMeetings.BeginAnimation(targetProp, animation);
            ucDelivery.BeginAnimation(targetProp, animation);
        }

        private void DisableNotifications()
        {
            if (colNotification.Width != new GridLength(0, GridUnitType.Star))
            {
                Kiosk.Animation.GridLengthAnimation ga = new Kiosk.Animation.GridLengthAnimation();
                ga.From = colNotification.Width;
                ga.To = new GridLength(0, GridUnitType.Star);
                ga.Duration = new Duration(TimeSpan.FromMilliseconds(250));

                colNotification.BeginAnimation(ColumnDefinition.WidthProperty, ga);
            }
        }

        private void EnableNotifications()
        {
            Kiosk.Animation.GridLengthAnimation ga = new Kiosk.Animation.GridLengthAnimation();
            ga.From = colNotification.Width;
            ga.To = new GridLength(1, GridUnitType.Star);
            ga.Duration = new Duration(TimeSpan.FromMilliseconds(250));

            colNotification.BeginAnimation(ColumnDefinition.WidthProperty, ga);
        }

        public bool ShowingPresentation
        {
            get
            {
                return _showingPresentation;
            }
        }
	    #endregion

        private void DataProvider_DataChanged(object sender, EventArgs e)
        {
            if (NotificationsActive() && _showingPresentation)
            {
                EnableNotifications();
            }
            else
            {
                DisableNotifications();
            }

        }

        private bool NotificationsActive()
        {
            return (NotificationReadOnlyList.GetAll().Where(n => 
                n.NotificationDate > DateTime.Now.AddMinutes(-30)
                && n.NotificationDate < DateTime.Now.AddHours(4)).Count() > 0);
        }

        public void RefreshNotifications()
        {
            deliveryDataProvider.Refresh();
            eventDataProvider.Refresh();
            meetingDataProvider.Refresh();
        }

        public void ShowPresentation()
        {
//            EnableNotifications();
            brdContainer.Child = ucPresentation;
            ResumePresentation();
        }

        public void PausePresentation()
        {
            if (this.ShowingPresentation)
            {
                ucPresentation.Pause();
                _showingPresentation = false;
            }
        }

        public void ResumePresentation()
        {
            if (!this.ShowingPresentation)
            {
                ucPresentation.Resume();
                _showingPresentation = true;

            }
        }
    }
}
